﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sahit.iSoilHotel.Portal.ServiceProxy.TavernRecordServiceReference;
using AutoMapper;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Record;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Customer;
using Sahit.iSoilHotel.Infrastructure.Web.Caching;
using Sahit.iSoilHotel.Infrastructure.Web.Events;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Filter;
using Sahit.iSoilHotel.ServiceProxy.Tavern.Models.Customer;

namespace Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Record
{
    public class TavernRecordServiceProxy : ITavernRecordServiceProxy
    {
        private TavernRecordServiceClient tavernService;
        private readonly ICacheManager cacheManager;
        private readonly IEventPublisher eventPublisher;

        public TavernRecordServiceProxy(ICacheManager cacheManager,
            IEventPublisher eventPublisher)
        {
            this.cacheManager = cacheManager;
            this.eventPublisher = eventPublisher;
        }

        public RecordModel Reserve(ReserveModel record)
        {
            var recordModel = (RecordModel)record;
            var recordDTO = Mapper.Map<RecordModel, RecordDTO>(recordModel);
            var mainCustomer = Mapper.Map<CustomerModel, CustomerDTO>(recordModel.MainCustomer);
            recordDTO.Customers = new CustomerDTO[] { mainCustomer };

            using (tavernService = new TavernRecordServiceClient())
            {
                tavernService.Reserve(recordDTO);
                eventPublisher.EntityInserted(record);
            }
            return record;
        }

        public List<RecordListModel> GetRecordsList(RecordFilterModel filter)
        {
                using (tavernService = new TavernRecordServiceClient())
                {
                    var filterDTO = Mapper.Map<RecordFilterDTO>(filter);
                    RecordDTO[] records = tavernService.GetRecords(filterDTO);
                    return Mapper.Map<List<RecordListModel>>(records);
                }
        }

        public List<CheckInModel> GetRecords(RecordFilterModel filter)
        {
            using (tavernService = new TavernRecordServiceClient())
            {
                var filterDTO = Mapper.Map<RecordFilterDTO>(filter);
                RecordDTO[] records = tavernService.GetRecords(filterDTO);
                return Mapper.Map<List<CheckInModel>>(records);
            }
        }

        public RecordModel CheckIn(CheckInModel record)
        {
            var recordModel = (RecordModel)record;
            var recordDTO = Mapper.Map<RecordModel, RecordDTO>(recordModel);
            var mainCustomer = Mapper.Map<CustomerModel, CustomerDTO>(recordModel.MainCustomer);
            recordDTO.Customers = new CustomerDTO[] { mainCustomer };
            using (tavernService = new TavernRecordServiceClient())
            {
                tavernService.CheckIn(recordDTO);
                eventPublisher.EntityUpdated(record);
            }
            return record;
        }

        public RecordModel CheckOut(CheckOutModel record)
        {
            var recordModel = (RecordModel)record;
            var recordDTO = Mapper.Map<RecordModel, RecordDTO>(recordModel);
            var mainCustomer = Mapper.Map<CustomerModel, CustomerDTO>(recordModel.MainCustomer);
            recordDTO.Customers = new CustomerDTO[] { mainCustomer };
            using (tavernService = new TavernRecordServiceClient())
            {
                tavernService.CheckOut(recordDTO);
                eventPublisher.EntityUpdated(record);
            }
            return record;
        }
    }
}
